package com.sxt.bus.controller;


import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxt.bus.domain.Customer;
import com.sxt.bus.service.ICustomerService;
import com.sxt.bus.vo.CustomerVo;
import com.sxt.sys.utils.DataGridView;
import com.sxt.sys.utils.ResultObj;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 老雷
 * @since 2019-08-14
 */
@RestController
@RequestMapping("/customer")
public class CustomerController {

	@Autowired
	private ICustomerService customerService;
	
	@RequestMapping("loadAllCustomer")
	public DataGridView loadAllCustomer(CustomerVo customerVo) {
		IPage<Customer> page = new Page<>(customerVo.getPage(),customerVo.getLimit());
		QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
		if(StringUtils.isNotBlank(customerVo.getCustomername())) {
			queryWrapper.like("customername", customerVo.getCustomername());
		}
		if(StringUtils.isNotBlank(customerVo.getAddress())) {
			queryWrapper.like("address", customerVo.getAddress());
		}
		if(customerVo.getAvailable()!=null) {
			queryWrapper.eq("available", customerVo.getAvailable());
		}
		this.customerService.page(page,queryWrapper);
		return new DataGridView(page.getTotal(),page.getRecords());
	}
	
	@RequestMapping("addCustomer")
	public ResultObj addCustomer(CustomerVo customerVo) {
		try {
			this.customerService.save(customerVo);
			return ResultObj.ADD_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.ADD_ERROR;
		}
	}
	
	
	@RequestMapping("deleteCustomer")
	public ResultObj deleteCustomer(Integer id) {
		try {
			this.customerService.removeById(id);
			return ResultObj.DELETE_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.DELETE_ERROR;
		}
	}
	
	
	@RequestMapping("updateRole")
	public ResultObj updateCustomer(CustomerVo customerVo) {
		try {
			this.customerService.updateById(customerVo);
			return ResultObj.UPDATE_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.UPDATE_ERROR;
		}
	}
}
